#include "mplayer.h"
#include <qnamespace.h>
#include <QSizePolicy>

extern QString g_AppPath;

mplayer::mplayer(QObject *parent) :
    mprocess(parent)
{
    m_pView = NULL;
    m_pView = new QWidget((QWidget*)parent);

    m_pView->setWindowFlags(Qt::WindowStaysOnTopHint);

    //关掉双缓冲机制，不然在QT4.7版本以上，在信号转换后显示切换时将出现白屏
    m_pView->setAttribute(Qt::WA_PaintOnScreen);



    m_playTimer = new QTimer(this);
    m_playTimer->setSingleShot(false);
    connect(m_playTimer, SIGNAL(timeout()), this, SLOT(timeout()));

    connect(this, SIGNAL(finished(int,QProcess::ExitStatus)),
         this, SLOT(processFinished(int,QProcess::ExitStatus)) );
}

mplayer::~mplayer()
{
    if (this->isRunning())
    {
        this->stop();
    }
    m_pView->hide();
    delete m_pView;
}


//void mplayer::setwid(QWidget* pView)
//{
//    m_pView = pView;

//    m_pView = new QWidget("",pView);
//    m_pView->setGeometry(pView->geometry());
//      m_pView->setGeometry(QRect(30, 30, 370, 335));
//}

void mplayer::setrect(QRect rect)
{
    m_rect.setLeft(rect.left());
    m_rect.setTop(rect.top());
    m_rect.setRight(rect.right());
    m_rect.setBottom(rect.bottom());
}

void mplayer::restart(QString fileName)
{
    QString strList;
    logger()->info("play:%1\n", fileName);
    m_fileName = fileName;


//#ifdef Q_WS_WIN
  //  QString path = tr("%1mplayer/mplayer.exe").arg(MPLAYPATH);
    //qDebug() << "mplayer path...........>>" << path;
//#endif

#ifdef CEPIDS
    QString path = tr("%1/mplayer/mplayer.exe").arg(g_AppPath);
//    logger()->info("mplayer path:%1\n", path);
#else
    QString path = tr("%1/mplayer/mplayer.exe").arg(g_installPath);
    addArgument(path);
#endif


//    strList.append("-input ");

        if(m_pView == NULL)
        {
           m_pView = new QWidget((QWidget*)parent());
//           if(m_pView != NULL)
//           {
//               //m_pView->setAttribute(WA_PaintUnclipped);
//               Qt::WindowType t = m_pView->windowType();
//               m_pView->setGeometry(m_rect);
//           }
        }

        if(m_pView != NULL)
        {
            //m_pView->setAttribute(WA_PaintUnclipped);
//            Qt::WindowType t = m_pView->windowType();
            m_pView->windowType();

            m_pView->setGeometry(m_rect);
        }


        m_pView->setGeometry(m_rect);

#ifdef Q_WS_WIN
        addArgument(QString::number(reinterpret_cast<qlonglong>(m_pView->winId())));
        addArgument("-vo");
        addArgument("directx:noaccel");
#else
//        strList.append(QString::number(m_pView->winId()));
//        strList.append(" -zoom ");
#endif
        strList.append("file=");
        writeToStdin(strList);

}

bool mplayer::play(QString fileName)
{
    //logger()->info("play:%1\n", fileName);

    QFile file(fileName);
    if (!file.exists())
    {
        logger()->warn("Video file \"%1\" can't find", fileName);
    }

    m_fileName = fileName;

    clearArguments();       //ivan @2011.09.29 add
    //    if( proc!= NULL)
    //    {
    //        proc->kill();
    //        proc = NULL;
    //    }

    //    QStringList args;

    //    proc = new QProcess((QObject*)this);
    //    proc->setProcessChannelMode( QProcess::MergedChannels );
    //    //       connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)),
    //    //            this, SLOT(procFinished()) );

    //    args<<"-slave";
    //    args<<"-quiet";
    //#ifdef Q_WS_WIN
    //    // reinterpret_cast<qlonglong> obligatoire, winId() ne se laissant pas convertir gentiment ;)
    //    //reinterpret_cast <qlonglong> mandatory winId () not allowing convert nicely;)
    //    args << "-wid" << QString::number(reinterpret_cast<qlonglong>(pView->winId()));
    //    args << "-vo" << "directx:noaccel";
    //#else
    //    // Sur linux, aucun driver n'a ?t? n?cessaire et pas de manip pour Wid :)
    //    //On linux, no driver has been necessary and no manip for Wid:)
    //    args << "-wid" << QString::number(pView->winId());
    //    args<<"-zoom";
    //#endif
    //    args<<fileName;

    //#ifdef Q_WS_WIN
    //    proc->start("./mplayer/mplayer.exe",args);
    //#else
    //    proc->start("mplayer",args);
    //#endif

//    if (arguments().isEmpty())
//    {

#ifdef Q_WS_WIN

#ifdef CEPIDS
    QString mplayerPath = tr("%1/mplayer/mplayer.exe").arg(g_AppPath);
#else
    QString mplayerPath = tr("%1/mplayer/mplayer.exe").arg(g_installPath);
#endif

    addArgument(mplayerPath);
#else
        addArgument("mplayer");
#endif
        addArgument("-slave");
        addArgument("-quiet");

        addArgument("-wid");



        if(m_pView == NULL)
        {
           m_pView = new QWidget((QWidget*)parent());
//           if(m_pView != NULL)
//           {
//               //m_pView->setAttribute(WA_PaintUnclipped);
//               Qt::WindowType t = m_pView->windowType();
//               m_pView->setGeometry(m_rect);
//           }
        }

        if(m_pView != NULL)
        {
            m_pView->windowType();
            m_pView->setGeometry(m_rect);
        }
        m_pView->setGeometry(m_rect);

        //測試用
//        m_playTimer->start(2 * 1000);
//        qDebug() << "m_rect============================================>" << m_rect;

#ifdef Q_WS_WIN
        addArgument(QString::number(reinterpret_cast<qlonglong>(m_pView->winId())));
        addArgument("-vo");
        addArgument("directx:noaccel");
#else
        addArgument(QString::number(m_pView->winId()));
//        addArgument("-loop");

        addArgument("-zoom");

#endif
        addArgument(fileName);

  //  }

        start();

        m_pView->show();            //ivan @2011.09.30 add
        return waitForStarted();
        //return true;
}


void mplayer::pause()
{
    writeToStdin("pause");
}

void mplayer::stop()
{
    m_playTimer->stop();
    writeToStdin("quit");
}

//离开，关闭播放
void mplayer::closePlay()
{
//    writeToStdin("quit");
    this->kill();
}

void mplayer::getseek()
{

}

void mplayer::setseek(int secs)
{
    if ( (isRunning()) && (secs!=0) )
    {
        writeToStdin("seek " + QString::number(secs) + " 0");
    }
}

void mplayer::getvolume()
{

}

void mplayer::setvolume()
{

}

void mplayer::hide()
{
    if (m_pView != NULL)
    {
        m_pView->hide();
    }
    m_playTimer->stop();
}

void mplayer::show()
{
    if (m_pView == NULL)
    {
        m_pView = new QWidget((QWidget*)parent());
    }

    m_pView->show();
}

void mplayer::seekToTimePos(const float seconds)
{
    QString cmd;
    cmd = tr("seek %1 2").arg(seconds);     //以时间点进行定位
//    cmd = tr("seek %1% 1").arg(seconds);  //以百分比进行定位

    writeToStdin(cmd);
}

void mplayer::getCurTimePos()
{
    writeToStdin("get_time_pos");           //获取当前时间点位置
//    writeToStdin("get_percent_pos");      //获取当百分比位置

}

void mplayer::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    //int code = exitCode;

    //logger()->info("play:%1 finish\n", m_fileName);
    emit mplayfinished();
}

void mplayer::timeout()
{
//    writeToStdin("seek 500 2\n");
    this->getCurTimePos();
    this->seekToTimePos(50);
}
